﻿
Imports std
Imports storoomc

Public Class memslices
    Implements storageInterface
    Private l As sequentialLock = Nothing
    Private impl As storageInterface = Nothing

    Public Sub start()
        If l Is Nothing Then
            l = New sequentialLock()
        End If
        If Not impl Is Nothing Then
            impl.close()
        End If
        Dim s As String = Nothing
        s = config.parameter(configNaming.memslices.name, _
                             configNaming.memslices.type, _
                             configNaming.memslices.defaultValue.type)
        If strsame(s, "cacheslices", False) Then
            Dim c As cacheslices = Nothing
            c = New cacheslices()
            c.start()
            impl = c
        Else
            Dim m As mapslices = Nothing
            m = New mapslices()
            m.start()
            impl = m
        End If
    End Sub

    Private Sub lock()
        l.wait()
    End Sub

    Private Sub unlock()
        l.release()
    End Sub

    Private Function lockwork(Of T, T2)(ByRef i1 As T, ByRef i2 As T2, ByVal work As _do(Of T, T2, Boolean)) As Boolean
        assert(Not work Is Nothing)
        Dim rtn As Boolean = False
        lock()
        rtn = work(i1, i2)
        unlock()
        Return rtn
    End Function

    Private Function lockwork(Of T)(ByRef i As T, ByVal work As _do(Of T, Boolean)) As Boolean
        assert(Not work Is Nothing)
        Dim w2 As _do(Of T, Object, Boolean) = _
            Function(ByRef i1 As T, ByRef i2 As Object) As Boolean
                Return work(i1)
            End Function
        Return lockwork(i, Nothing, w2)
    End Function

    Private Function lockwork(ByVal work As _do(Of Boolean)) As Boolean
        assert(Not work Is Nothing)
        Dim w2 As _do(Of Object, Object, Boolean) = _
            Function(ByRef i1 As Object, ByRef i2 As Object) As Boolean
                Return work()
            End Function
        Return lockwork(Nothing, Nothing, w2)
    End Function

    Public Function capacity(ByRef cap As UInt64) As Boolean Implements storageInterface.capacity
        Return lockwork(cap, Function(ByRef c As UInt64) As Boolean
                                 Return impl.capacity(c)
                             End Function)
    End Function

    Public Function close() As Boolean Implements storageInterface.close
        Return lockwork(Function() As Boolean
                            Return impl.close()
                        End Function)
    End Function

    Public Function delete(ByVal key As String) As Boolean Implements storageInterface.delete
        Return lockwork(Function() As Boolean
                            Return impl.delete(key)
                        End Function)
    End Function

    Public Function empty() As Boolean Implements storageInterface.empty
        Return lockwork(Function() As Boolean
                            Return impl.empty()
                        End Function)
    End Function

    Public Function full() As Boolean Implements storageInterface.full
        Return lockwork(Function() As Boolean
                            Return impl.full()
                        End Function)
    End Function

    Public Function health() As Boolean Implements storageInterface.health
        Return lockwork(Function() As Boolean
                            Return Not impl Is Nothing AndAlso impl.health()
                        End Function)
    End Function

    Public Function keycount(ByRef count As UInt64) As Boolean Implements storageInterface.keycount
        Return lockwork(count, Function(ByRef c As UInt64) As Boolean
                                   Return impl.keycount(c)
                               End Function)
    End Function

    Public Function list(ByRef keys As vector(Of String)) As Boolean Implements storageInterface.list
        Return lockwork(keys, Function(ByRef k As vector(Of String)) As Boolean
                                  Return impl.list(k)
                              End Function)
    End Function

    Public Function modify(ByVal key As String, ByVal buff As bytes) As Boolean Implements storageInterface.modify
        Return lockwork(Function() As Boolean
                            Return impl.modify(key, buff)
                        End Function)
    End Function

    Public Function open() As Boolean Implements storageInterface.open
        Return lockwork(Function() As Boolean
                            Return impl.open()
                        End Function)
    End Function

    Public Function read(ByVal key As String, ByRef buff As bytes) As Boolean Implements storageInterface.read
        Return lockwork(buff, Function(ByRef b As bytes) As Boolean
                                  Return impl.read(key, b)
                              End Function)
    End Function

    Public Function retire() As Boolean Implements storageInterface.retire
        Return lockwork(Function() As Boolean
                            Return impl.retire()
                        End Function)
    End Function

    Public Function seek(ByVal key As String) As Boolean Implements storageInterface.seek
        Return lockwork(Function() As Boolean
                            Return impl.seek(key)
                        End Function)
    End Function

    Public Function sizeof(ByVal key As String, ByRef size As UInt64) As Boolean Implements storageInterface.sizeof
        Return lockwork(size, Function(ByRef s As UInt64) As Boolean
                                  Return impl.sizeof(key, s)
                              End Function)
    End Function

    Public Function status(ByRef allSlicesCount As UInt64, _
                           ByRef unhealthSlicesCount As UInt64) As Boolean Implements storageInterface.status
        Return lockwork(allSlicesCount, unhealthSlicesCount, Function(ByRef a As UInt64, ByRef u As UInt64) As Boolean
                                                                 Return impl.status(a, u)
                                                             End Function)
    End Function

    Public Function sync(ByVal desthost As String, ByVal destport As Int32) As Boolean Implements storageInterface.sync
        Return lockwork(Function() As Boolean
                            Return impl.sync(desthost, destport)
                        End Function)
    End Function

    Public Function uniqueWrite(ByVal key As String, ByVal buff As bytes) As Boolean _
        Implements storageInterface.uniqueWrite
        Return lockwork(Function() As Boolean
                            Return impl.uniqueWrite(key, buff)
                        End Function)
    End Function

    Public Function valuesize(ByRef size As UInt64) As Boolean Implements storageInterface.valuesize
        Return lockwork(size, Function(ByRef s As UInt64) As Boolean
                                  Return impl.valuesize(s)
                              End Function)
    End Function

    Public Function write(ByVal key As String, ByVal buff As bytes) As Boolean Implements storageInterface.write
        Return lockwork(Function() As Boolean
                            Return impl.write(key, buff)
                        End Function)
    End Function
End Class
